package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags="处理员工请求的控制层")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌   载荷--自定义信息--map   jwt底层是根据秘钥和算法完成签名计算
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
                //    "empId",  员工id
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),//秘钥
                jwtProperties.getAdminTtl(),//过期时间
                claims);//载荷

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
       // @Builder 注解    对象可以根据 构建者来完成构建
          //  类.bulider()方法获取到了对象构建者-- .属性(值).属性(值)
        //    .bulid()返回一个 构建出来的对象


        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工登出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     *  新增员工信息
     */
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
      log.info("新增员工基础信息:"+employeeDTO);
       //service完成
        employeeService.save(employeeDTO);

        return Result.success();


    }
//  Result<PageResult>   响应结果中 数据是 分页对象

    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
            log.info("员工分页查询参数:{}",employeePageQueryDTO);
            //找service 查询得到分页对象
        PageResult pageResult = employeeService.page(employeePageQueryDTO);

        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
          log.info("启用禁用员工id:"+id+" 修改的状态是:"+status);
          employeeService.startOrStop(status,id);

          return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        //service完成查询
        log.info("根据id查询："+id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     */
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑的员工信息:{}",employeeDTO);

        employeeService.update(employeeDTO);//employeeDTO 接收到前端传递的数据

        return Result.success();
    }

}
